<?php

namespace App\Http\Controllers\pushcontroller;
use App\Http\Controllers\Controller;
use App\Models\Push;
use App\Services\PushServiceInterFace;
use Illuminate\Http\Request;
use Log;
use App\Pojo\Response;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Support\Facades\Auth;
class PushController extends Controller{
    private $pushService;

    public function __construct(PushServiceInterFace $pushServiceInterFace)
    {
        $this->pushService = $pushServiceInterFace;
    }
    public function friendCallback(Request $request){
            Log::info("新增好友回调");
            Log::info( $request->all());
            
        return response()->json([
            'code' => 200,
            'msg' => 'success'
        ]); 
    }

    public function msgCallback(Request $request){
            Log::info("消息回调");
            Log::info( $request->all());
            
            return response()->json([
                'code' => 200,
                'msg' => 'success'
            ]); 
        }
    public function userCallback(Request $request){
        Log::info("添加用户回调");
        Log::info( $request->all());
        
        return response()->json([
            'code' => 200,
            'msg' => 'success'
        ]); 
    }

    public function getfriendlist(Request $request){
        
       
    }

    public function getQrCode(Request $request){
        return $this->pushService->getQrCode();
       
    }

    public function getWechatQrCode(Request $request){
        // return $this->pushService->getWechatQrCode();
    }


    public function getAccessKey(Request $request){
        return $this->pushService->getAccessKey();
    }

    public function delfriend(Request $request){
        
       
    }

    public function getPushToken(Request $request){
        $push = Push::where('user_id', Auth::id())->first();
        if ($push) {
            return Response::success($push);
        }
        return Response::error();
    }

    public function updatePushToken(Request $request){
        $request->validate([
            'token' => 'required|string|max:255',
        ]);
        $token = $request->input('token');
        
        $push = Push::where('user_id', Auth::id())->first();
        
        if ($push) {
            $push->update(['token' => $token]);
        } else {
            Push::create([
                'user_id' => Auth::id(),
                'token' => $token
            ]);
        }
        
        return response()->json([
            'code' => 200,
            'msg' => 'success'
        ]);
    }

    public function sendTestMsg(Request $request){
        $request->validate([
            'token' => 'required|string|max:255',
            'content' => 'required|string|max:255',
        ]);
        $token = $request->input('token');
        $content = $request->input('content');
        $response = $this->pushService->sendTestMsg($token, $content);
        return $response;
    }
}